package com.soap.desensitize.factory.posterity;

import com.soap.desensitize.encry.IDesensitization;
import com.soap.desensitize.exception.factory.FactoryException;
import com.soap.desensitize.exception.factory.PosterityFactoryException;
import com.soap.desensitize.exception.factory.StrategyFactoryException;
import com.soap.desensitize.posterity.AbstractPosterity;
import com.soap.desensitize.posterity.EPosterity;
import com.soap.desensitize.posterity.string.IdCardDesensitization;
import com.soap.desensitize.posterity.string.PhoneDesensitization;
import com.soap.desensitize.posterity.string.StringDesensitization;

/**
 * @desc 字符串脱敏子孙后代类工厂
 */
public class StringDesFactory {

    /**
     * @desc 根据Class 生产字符串脱敏的功能拓展类
     * @param cla
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static AbstractPosterity create(Class<? extends AbstractPosterity> cla) throws InstantiationException, IllegalAccessException {
        return cla.newInstance();
    }
    /**
     * @desc 根据Class 生产字符串脱敏的功能拓展类
     * @param cla
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    public static AbstractPosterity create(Class<? extends AbstractPosterity> cla, IDesensitization encry) throws InstantiationException, IllegalAccessException {
        AbstractPosterity posterity = cla.newInstance();
        posterity.setEncry(encry);
        return posterity;
    }

    /**
     * @desc 根据类路径生产字符串脱敏的功能拓展类
     * @param classStr
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractPosterity create(String classStr) throws InstantiationException, IllegalAccessException, PosterityFactoryException {
        switch (classStr){
            case "com.rj.bd.posterity.string.IdCardDesensitization":
                return IdCardDesensitization.getInstance();
            case "com.rj.bd.posterity.string.PhoneDesensitization":
                return PhoneDesensitization.getInstance();
            case "com.rj.bd.posterity.string.StringDesensitization":
                return StringDesensitization.getInstance();
        }
        throw new PosterityFactoryException("没有找到这个子孙后代类 "+classStr);
    }

    /**
     * @desc 根据枚举生产字符串脱敏的功能拓展类
     * @param ePosterity 子孙后代类-枚举
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws StrategyFactoryException
     */
    public static AbstractPosterity create(EPosterity ePosterity) throws FactoryException {
        try {
            return create(ePosterity.getClassName());
        } catch (InstantiationException e) {
            throw new FactoryException("实例化异常 InstantiationException");
        } catch (IllegalAccessException e) {
            throw new FactoryException("非法访问异常 IllegalAccessException");
        } catch (PosterityFactoryException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @desc 创建
     * @param ePosterity
     * @param encry
     * @return
     */
    public static AbstractPosterity create(EPosterity ePosterity, IDesensitization encry) throws FactoryException {
        AbstractPosterity posterity = null;
        try {
            posterity = create(ePosterity.getClassName());
        } catch (InstantiationException e) {
            throw new FactoryException("实例化异常 InstantiationException");
        } catch (IllegalAccessException e) {
            throw new FactoryException("非法访问异常 IllegalAccessException");
        } catch (PosterityFactoryException e) {
            throw new RuntimeException(e);
        }
        posterity.setEncry(encry);
        return posterity;
    }
}
